<?php

class CommodityCategory
{
    /** @var int */
    protected $id = 0;
    /** @var int */
    protected $fatherId = 0;
    /** @var string */
    protected $name = '';
    /** @var string */
    protected $iconUrl = '';

    public function countCommodity()
    {
        return Commodity_Model::countCommodityByCommodityCategoryIdArray(array($this->getId()));
    }

    public function countDescendantCommodity()
    {
        $thatDescendantCommodityCategoryIdArray = CommodityCategory_Model::getDescendantCommodityCategoryIdArrayById($this->getId());
        return Commodity_Model::countCommodityByCommodityCategoryIdArray($thatDescendantCommodityCategoryIdArray);
    }

    public function countDescendantCommodityWithSelf()
    {
        return $this->countCommodity() + $this->countDescendantCommodity();
    }

    public function getChildCommodityCategory()
    {
        return CommodityCategory_Model::getChildrenCommodityCategoryArrayById($this->getId());
    }

    public function getMasterCommodityCategoryId()
    {
        $nowCat         = $this;
        while ($nowCat->getFatherId() != 0) {
            $nowCat = CommodityCategory_Model::getCommodityCategoryById($nowCat->getFatherId());
        }
        return $nowCat->getId();
    }

    /**
     * @return array
     */
    public function getDataArray()
    {
        return array(
            'id'        => $this->getId(),
            'father_id' => $this->getFatherId(),
            'name'      => $this->getName(),
            'icon'      => $this->getIconUrl(),
        );
    }

    /**
     * @param int $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return int
     */
    public function getFatherId()
    {
        return $this->fatherId;
    }

    /**
     * @param int $fatherId
     */
    public function setFatherId($fatherId)
    {
        $this->fatherId = $fatherId;
    }

    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @return string
     */
    public function getIconUrl()
    {
        return $this->iconUrl;
    }

    /**
     * @param string $iconUrl
     */
    public function setIconUrl($iconUrl)
    {
        $this->iconUrl = $iconUrl;
    }

}